package com.authine.cloudpivot.ext.utils.system;

import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.system.OutApiClientDto;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.common.util.ObjectMapperUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author hxd
 * @Date 2021/9/27 17:04
 * @Description 对外api访问客户端 工具
 **/
public class OutApiClientApiUtils {

    /**
     * out-api:根据固买系统用户名获取数据
     */
    public final static String getByGomroUsername = "/OutApiClientService/getByGomroUsername";


    private static String getUrl(String apiUrl) {
        return HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, "/application/invoke?action=" + apiUrl);
    }

    /**
     * 根据固买系统用户名获取数据
     * 需要是登录状态
     *
     * @param gomroUserName
     * @return
     */
    public static OutApiClientDto getByGomroUsername(String gomroUserName) {
//        String url = getUrl(getByGomroUsername);
//        Map<String, Object> userMap = Maps.newHashMap();
//        userMap.put("gomro_username", gomroUserName);
//        return HttpClients.execute(url, userMap, OutApiClientDto.class);

//        PageImpl<OutApiClientDto> page = BoQueryUtils.Builder(ModelConst.T_OUT_API_CLIENT).condition().eq("gomro_username", gomroUserName)
//                .rpc(AppConst.SYSTEM_MANAGEMENT).listBO(OutApiClientDto.class);
//
//        return page.getContent().stream().findFirst().orElse(null);

        Map<String, Object> map = getMapByGomroUserName(gomroUserName);

        OutApiClientDto outApiClientDto = ObjectMapperUtils.fromJSON(ObjectMapperUtils.toJSONString(map), OutApiClientDto.class);


        return outApiClientDto;

    }

    /**
     * 获取第三方客户端,
     * 直接从数据库获取
     *
     * @param gomroUserName
     * @return
     */
    public static Map<String, Object> getMapByGomroUserName(String gomroUserName) {
        List<Map<String, Object>> list = JdbcTemplateUtils.builder(AppConst.SYSTEM_MANAGEMENT, ModelConst.T_OUT_API_CLIENT)
                .selectField(" * ").eq("gomro_username", gomroUserName)
                .queryForList();

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

}
